package com.example.xpvideo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xpvideo.entity.LsItemCategory;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.service.LsItemCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hjt
 * @create 2021-09-01 17:40
 * @describe：商品分类配置-本地生活商品-分类名称(项目分类)
 */
@RestController
@RequestMapping("itemCategory")
public class ItemCategoryController {
    @Autowired
    private LsItemCategoryService itemCategoryService;

    /**
     * 添加分类名称
     *
     * @param itemCategory
     * @param result
     * @return
     */
    @PostMapping("insertItemCategory")
    public ResponseResult insertItemCategory(@RequestBody @Validated  LsItemCategory itemCategory, BindingResult result) {
        if (result.hasErrors()) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, result.getTarget());
        }
        boolean save = itemCategoryService.save(itemCategory);
        return save ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20002, null);
    }

    /**
     * 编辑项目分类名称
     *
     * @param itemCategory
     * @param result
     * @return
     */
    @PutMapping("editItemCategory")
    public ResponseResult editItemCategory(@RequestBody @Validated LsItemCategory itemCategory, BindingResult result) {
        if (result.hasErrors()) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, result.getTarget());
        }
        boolean save = itemCategoryService.updateById(itemCategory);
        return save ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20002, null);
    }

    /**
     * 删除项目分类名称
     * @param id
     * @return
     */
    @DeleteMapping("delItemCategory/{id}")
    public ResponseResult delItemCategory(@PathVariable("id") Integer id) {
        LsItemCategory itemCategory = new LsItemCategory();
        itemCategory.setId(id);
        itemCategory.setDel(1);
        boolean b = itemCategoryService.updateById(itemCategory);
        return b ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
    }

    /**
     *查询所分类名称
     * @return
     */
    @GetMapping("findItemCategory")
    public ResponseResult findItemCategory() {
        List<LsItemCategory> categories = itemCategoryService.list(new QueryWrapper<LsItemCategory>().eq("del", 0));
        return ResponseResult.responseResult(0, categories);
    }
}
